<template>
  <div>
    <a-form layout="horizontal" :form="form">
      <a-form-item
        label="付款账户"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <b>{{ step.payAccount }}</b>
      </a-form-item>
      <a-form-item
        label="收款账户"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <b>{{
          (step.receiverAccount.payType === "bank" ? "银行 " : "支付宝 ") +
            step.receiverAccount.number
        }}</b>
      </a-form-item>
      <a-form-item
        label="密码"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-input
          placeholder="请输入密码"
          type="password"
          v-decorator="[
            'password',
            {
              rules: [{ required: true, min: 6, message: '请输入密码' }]
            }
          ]"
        >
        </a-input>
      </a-form-item>
      <a-form-item
        :label-col="formItemOffsetLayout.labelCol"
        :wrapper-col="formItemOffsetLayout.wrapperCol"
      >
        <a-button type="primary" @click="handleSubmit" style="width: 100%"
          >提交</a-button
        >
      </a-form-item>
      <a-form-item
        :label-col="formItemOffsetLayout.labelCol"
        :wrapper-col="formItemOffsetLayout.wrapperCol"
      >
        <a-button @click="toPrev" style="width: 100%">上一步</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";

export default {
  name: "Confirm",
  data() {
    // 必须要在template中使用“form前”，以及vue实例化“this后”创建form实例所以要在return前创建，也可以在生命周期beforeCreate前创建
    this.form = this.$form.createForm(this, { name: "stepOne" });
    return {
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 10 }
      },
      formItemOffsetLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 4, offset: 4 }
      }
    };
  },
  computed: {
    ...mapState("form", {
      step: state => state.step
    })
  },
  methods: {
    ...mapActions("form", ["submitStepForm"]),
    toPrev() {
      this.$router.push({ name: "info" });
    },
    handleSubmit() {
      let _this = this;
      this.form.validateFields(function(error, values) {
        if (!error) {
          // 第二步完成后发送请求，是异步的所以先dispatch 触发action
          _this.submitStepForm({ params: { ..._this.step, ...values } });
        }
      });
    }
  }
};
</script>

<style lang="less"></style>
